package cn.sunlelin.educationserver.service.Impl;

import cn.sunlelin.educationserver.entity.Chapter;
import cn.sunlelin.educationserver.mapper.ChapterMapper;
import cn.sunlelin.educationserver.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ChapterServiceImpl implements ChapterService {
    @Autowired
    private ChapterMapper chapterMapper;
    @Override
    public Boolean createChapter(Chapter chapter) {
        chapter.setCreateTime(new Date());
        chapterMapper.createChapter(chapter);
        return true;
    }

    @Override
    public Boolean updateChapter(Chapter chapter) {
        if(chapterMapper.selectChapterById(chapter.getChapterId())==null)
            return false;
        chapter.setUpdateTime(new Date());
        chapterMapper.updateChapter(chapter);
        return true;
    }

    @Override
    public Chapter selectChapterById(Integer chapterId) {
        return chapterMapper.selectChapterById(chapterId);
    }

    @Override
    public List<Chapter> selectChapterByCourseId(Integer courseId) {
        return chapterMapper.selectChapterByCourseId(courseId);
    }

    @Override
    public Boolean deleteChapter(Integer chapterId) {
        if(chapterMapper.selectChapterById(chapterId)==null)
            return false;
        Chapter chapter=new Chapter();
        chapter.setChapterId(chapterId);
        chapter.setUpdateTime(new Date());
        chapterMapper.deleteChapter(chapter);
        return true;
    }
}
